package com.ruoyi.business.rescueTask.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.business.rescueTask.domain.RescueTask;
import com.ruoyi.business.rescueTask.service.IRescueTaskService;
import com.ruoyi.business.rescueTask.service.NearbyService;
import com.ruoyi.business.rescueTask.utils.TencentMapUtil;
import org.springframework.data.geo.*;

import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.business.rescueTask.mapper.RescueTaskMapper;

import javax.annotation.Resource;

/**
 * 救援任务
 * Service业务层处理
 *
 * @author lml
 * @date 2025-02-21
 */
@Service
public class RescueTaskServiceImpl implements IRescueTaskService {
    @Resource
    private RescueTaskMapper rescueTaskMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private NearbyService nearbyService;

    double longitude;
    double latitude;


    /**
     * 查询救援任务
     *
     * @param id 救援任务主键
     * @return 救援任务
     */
    @Override
    public RescueTask selectRescueTaskById(Long id) {
        return rescueTaskMapper.selectRescueTaskById(id);
    }

    /**
     * 查询救援任务列表
     *
     * @param rescueTask 救援任务
     * @return 救援任务
     */
    @Override
    public List<RescueTask> selectRescueTaskList(RescueTask rescueTask) {
        return rescueTaskMapper.selectRescueTaskList(rescueTask);
    }

    /**
     * 新增救援任务
     *
     * @param rescueTask 救援任务
     * @return 结果
     */
    @Override
    public int insertRescueTask(RescueTask rescueTask) {
        Date date = new Date();
        rescueTask.setTime(date);
        String position = rescueTask.getPosition();
        String[] coordinates = TencentMapUtil.getCoordinates(position);
        System.out.println("定位数据=========：" + coordinates);
        if (coordinates != null) {
            longitude = Double.parseDouble(coordinates[0]);
            latitude = Double.parseDouble(coordinates[1]);
            // 处理经纬度，如保存到数据库等
            System.out.println("经度: " + longitude + ", 纬度: " + latitude);
            if (longitude != 0 && latitude != 0) {
                //模拟商家位置数据
//                nearbyService.nearbyJoin();
                //查询50KM范围内的商家
                GeoResults<RedisGeoCommands.GeoLocation<Object>> geoResults = (GeoResults<RedisGeoCommands.GeoLocation<Object>>) nearbyService.nearbyLeave(longitude, latitude);
                System.out.println("经纬度" + longitude + "," + latitude);
                System.out.println("50km的查询结果：" + geoResults);
                //遍历结果集拿到商家ID、距中心点的距离
                List<String> stringList  = new ArrayList<>();
                List<Double> distanceList = new ArrayList<>();//存放距中心点距离
                List<Long> idList = new ArrayList<>();//存放商家id
                for (GeoResult<RedisGeoCommands.GeoLocation<Object>> geoResult : geoResults) {
                    RedisGeoCommands.GeoLocation<Object> geoLocation = geoResult.getContent();
                    String name = (String) geoLocation.getName();
                    stringList .add(name);
                    double distance = geoResult.getDistance().getValue();
                    distanceList.add(distance);
                    System.out.println("Name: " + name + ", Distance: " + distance + " km");
                }
                for (String str : stringList ) {
                    idList.add(Long.parseLong(str));
                }
            }
        }

        return rescueTaskMapper.insertRescueTask(rescueTask);
    }


    /**
     * 修改救援任务
     *
     * @param rescueTask 救援任务
     * @return 结果
     */
    @Override
    public int updateRescueTask(RescueTask rescueTask) {
        return rescueTaskMapper.updateRescueTask(rescueTask);
    }

    /**
     * 批量删除救援任务
     *
     * @param ids 需要删除的救援任务
     *            主键
     * @return 结果
     */
    @Override
    public int deleteRescueTaskByIds(Long[] ids) {
        return rescueTaskMapper.deleteRescueTaskByIds(ids);
    }

    /**
     * 删除救援任务
     * 信息
     *
     * @param id 救援任务
     *           主键
     * @return 结果
     */
    @Override
    public int deleteRescueTaskById(Long id) {
        return rescueTaskMapper.deleteRescueTaskById(id);
    }
}
